﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;
using ZBJF.Warranty.BLL.Cached;
using ZBJF.Warranty.BLL.SMS;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Model.CommonObject.PinanAPIEntity;
using ZBJF.Warranty.PinanAPI;
using ZBJF.Warranty.PinanAPI.Model;

namespace ZBJF.Warranty.ContsoleTest
{

    class Program
    {
        static string PostJSON(string url, string strPost)
        {
            string result = "";

            StreamWriter myWriter = null;
            HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
            objRequest.Method = "POST";

            objRequest.ContentType = "text/json"; //提交xml 
            try
            {
                myWriter = new StreamWriter(objRequest.GetRequestStream());
                myWriter.Write(strPost);
            }
            catch (Exception e)
            {
                return e.Message;
            }
            finally
            {
                myWriter.Close();
            }
            try
            {
                HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
                using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
                {
                    result = sr.ReadToEnd();
                    sr.Close();
                }
            }
            catch (Exception e)
            {
                if (e.Message.IndexOf("401") > -1)
                    return "401";
                else if (e.Message.IndexOf("500") > -1)
                    return "500";
            }
            return result;
        }
        static void Main(string[] args)
        {
            CommonMethod.SaveBase64Image(AppConfigCached.GetAppConfig().UploadFileVirtualPath, "12121212");
            Console.WriteLine(AppDomain.CurrentDomain.BaseDirectory);
            Console.WriteLine(DateTime.Now.ToString("yyyy\\MM\\dd"));
            Console.ReadLine();
            var jsons =
                "{\"effDate\":\"20180120000000\",\"policyNo\":\"59140909190012249093\",\"matuDate\":\"20190119235959\"}";
            var entity = (PinanIndentityReturnEntity)JSON.Decode(jsons, (typeof(PinanIndentityReturnEntity)));
            if (entity != null && entity.ret != null)
            {
                
            }
            else
            {
                var ht = JSON.Decode(jsons) as Hashtable;
                if (ht != null)
                {
                    if (ht["policyNo"] != null)
                    {
                        if (!string.IsNullOrEmpty(ht["errorMsg"].ToString()))
                        {
                            if (ht["effDate"] != null)
                            {
                                //order.Effective = true;
                                //order.EffectiveDate = CommonMethod.ConvertToDateTime(ht["effDate"].ToString());
                                //order.ServiceStartTime = order.EffectiveDate;
                                //order.ServiceEndTime = CommonMethod.ConvertToDateTime(ht["matuDate"].ToString());
                            }
                            //return true;
                        }
                    }
                    if (ht["errorMsg"] != null)
                    {
                        //msg = ht["errorMsg"].ToString();
                    }
                }
                else
                {
                    //msg = "json is null";
                }
                //return false;
            }


            ValidateCar();
            ActiveInsurance();
            SendInsurance();
            var pwd = CommonMethod.JieMi("k3QkcIkHsNM22U0njtl2Sw==");
            Console.WriteLine(pwd);
            Console.ReadLine();
            TestSendSMS();
            GetProvince();
            GetCity();
            //CommonMethod.SaveBase64Image("");
            //Console.WriteLine(CommonMethod.JiaMi("111111"));
            var json = "{\"CardNo\":\"1\",\"CardPassword\":\"2\"}";
            Console.WriteLine(SecuritySignHelper.DESEncrypt("{\"Phone\":\"13510449969\"}", "12345678"));
            Console.WriteLine(SecuritySignHelper.DESDecrypt("K7AD0/YH8kX7ZMrCYcTxb12fL2ZWqEwW", "12345678"));
            //SendSMS();
            SendVerificationCode();
        }
        static void SendSMS()
        {
            var obj = new
            {
                Phones = "14789762489",
                Message = "API 测试"
            };
            const string skeyStr = "12345678";
            var body = SecuritySignHelper.DESEncrypt(JSON.Encode(obj), skeyStr);
            //http://112.74.68.23:9999/Bus/productquery/1.0/20170418170347/5580ff94a7e67eca4f141e9af0e7c953/Vetech
            var sign = SecuritySignHelper.GetMD5_32(string.Format("{0}{1}{2}{3}{4}{5}", "zbjf", "1.0",
                "AppApi", "20170918170347", skeyStr,
                body.Length));
            var url = string.Format("http://120.78.80.233:9999/AppApi/SendSMS/1.0/20170918170347/{0}/app", sign);

            Console.WriteLine(PostJSON(url, body));
        }
        static void SendVerificationCode()
        {
            var obj = new
            {
                Phone = "14789762489"
            };
            const string skeyStr = "12345678";
            var body = SecuritySignHelper.DESEncrypt(JSON.Encode(obj), skeyStr);
            //http://112.74.68.23:9999/Bus/productquery/1.0/20170418170347/5580ff94a7e67eca4f141e9af0e7c953/Vetech
            var sign = SecuritySignHelper.GetMD5_32(string.Format("{0}{1}{2}{3}{4}{5}", "zbjf", "1.0",
                "AppApi", "20170918170347", skeyStr,
                body.Length));
            var url = string.Format("http://120.78.80.233:9999/AppApi/SendVerificationCode/1.0/20170918170347/{0}/app", sign);

            Console.WriteLine(PostJSON(url, body));
        }
        public static void TestSendSMS()
        {
            string msg = string.Empty;
            SMSHelper.SendVerificationCodeSMS("14789762489", "123456", ref msg);

            var app = AppConfigCached.GetAppConfig();
            var res = string.Empty;
        }
        public static void GetProvince()
        {
            const string skeyStr = "12345678";
            var body = SecuritySignHelper.DESEncrypt(string.Empty, skeyStr);
            //http://112.74.68.23:9999/Bus/GetProvince/1.0/20170418170347/5580ff94a7e67eca4f141e9af0e7c953/Vetech
            var sign = SecuritySignHelper.GetMD5_32(string.Format("{0}{1}{2}{3}{4}{5}", "zbjf", "1.0",
                "AppApi", "20170918170347", skeyStr,
                body.Length));
            var url = string.Format("http://120.78.80.233:9999/AppApi/GetProvince/1.0/20170918170347/{0}/app", sign);

            Console.WriteLine(PostJSON(url, body));
        }

        public static void GetCity()
        {
            var obj = new
            {
                ProvinceId = 13
            };
            const string skeyStr = "12345678";
            var body = SecuritySignHelper.DESEncrypt(JSON.Encode(obj), skeyStr);
            //http://112.74.68.23:9999/Bus/GetCity/1.0/20170418170347/5580ff94a7e67eca4f141e9af0e7c953/Vetech
            var sign = SecuritySignHelper.GetMD5_32(string.Format("{0}{1}{2}{3}{4}{5}", "zbjf", "1.0",
                "AppApi", "20170918170347", skeyStr,
                body.Length));
            var url = string.Format("http://120.78.80.233:9999/AppApi/GetCity/1.0/20170918170347/{0}/app", sign);

            Console.WriteLine(PostJSON(url, body));
        }

        public static void SendInsurance()
        {
            var obj = new InsuranceInfo
            {
                #region  Init INFO
                partnerName = "P_ZBJFTESTING",
                departmentCode = "20468",
                applicantInfo = new ApplicantInfo
                {
                    name = "测试翼支付",
                    birthday = "1984-08-12",
                    sexCode = "sexCode",
                    certificateNo = "513328198408122477",
                    certificateType = "01",
                    address = "北京",
                    postcode = "1",
                    mobileTelephone = "13311002469",
                    personnelType = "1"
                },
                productInfoList = new List<ProductInfo>
                {
                    new ProductInfo
                    {
                        baseInfo = new ProductBaseInfo
                        {
                            transactionNo = DateTime.Now.ToString("yyyyMMddHHmmss"),
                            totalActualPremium = "58",
                            productCode = "产品代码",
                            insuranceBeginDate = "2017-09-19 08:08:00",
                            insuranceEndDate = "2018-09-19 08:08:00",
                            coinsuranceMark = "1",
                            businessType = "1"
                        },
                        insurantInfoList = new List<ProductInsuranceInfo>
                        {
                            new ProductInsuranceInfo
                            {
                                name = "测试翼支付",
                                birthday = "1984-08-12",
                                sexCode = "F",
                                certificateNo = "513328198408122477",
                                certificateType = "01",
                                address = "北京",
                                postcode = "1",
                                mobileTelephone = "13311002469",
                                personnelType = "1",
                                //age= 33
                            }
                        },
                        riskGroupInfoList = new List<ProductRiskGroupInfo>
                        {
                            new ProductRiskGroupInfo
                            {
                                applyNum = 1,
                                productPackageType = 1,
                                riskPropertyInfoList = new List<ProductRiskPropertyInfo>
                                {
                                    new ProductRiskPropertyInfo
                                    {
                                        riskPropertyMap = new
                                        {
                                            brand = "苹果",
                                            brandType = "01",
                                            imeiCode = "6789987698",
                                            productType = "01",
                                            birthday = "1984-08-12",
                                            personnelName = "测试翼支付",
                                            relation = "本人",
                                            address = "北京"
                                        }
                                    }
                                }
                            }
                        }
                    },
                }
                #endregion
            };
            var url = string.Format("https://test-api.pingan.com.cn:20443/open/appsvr/property/standardApplyPolicyRiskPropertyNoPay?access_token={0}&request_id={1}", "312FF5388CE64AC197C854C3FF6C3121", "open/appsvr/property/standardApplyPolicyRiskPropertyNoPay" + DateTime.Now.ToString("yyyyMMddHHmmss"));
            var res = PostJSON(url, JSON.Encode(obj));
            Console.WriteLine(res);
        }

        public static void ActiveInsurance()
        {
            var obj = new
            {
                partnerCode = "P_SPXZZK_PE",
                tranCode = "keyPointspxInfo",
                OPEN_SERVICE_REQUEST = new
                {
                    operType = "activeSics",
                    cardNo = "0113129000001201",
                    password = "051701",
                    callId = "propInsuActive",
                    holderName = "YVFDHJH",
                    holderSex = "M",
                    holderBirthday = "1999-10-30",
                    holderIdNo = "321322199910301817",
                    holderIdType = "01",
                    holderBloodtype = "1",
                    holderMobile = "0111738000000000",
                    isFillHolderMobile = "123456",
                    holderEmail = "123456@QQ.COM",
                    isFillHolderEmail = "0111738000000000",
                    callName = "搬砖",
                    relatedKey = "56111111111",
                    name = "YVFDHJH",
                    channel = "11",
                    sex = "M",
                    birthday = "1999-10-30",
                    idNo = "321322199910301817",
                    idType = "01",
                    mobile = "0111738000000000",
                    email = "123456@QQ.COM",
                    address = "0111738000000000",
                    petType = "123456",
                    petBreed = "0111738000000000",
                    petAge = "123456",
                    petHairColour = "123456",
                    identityNo = "0111738000000000",
                    petName = "123456",
                    petSex = "0111738000000000"
                }
            };
            var url = string.Format("https://test-api.pingan.com.cn:20443/open/appsvr/property/outerToPingAn?access_token={0}&request_id={1}", "14E255E7CF394AF7BD7C9278E887E9AB", "open/appsvr/property/outerToPingAn" + DateTime.Now.ToString("yyyyMMddHHmmss"));
            var res = PostJSON(url, JSON.Encode(obj));
            Console.WriteLine(res);
        }

        public static void ValidateCar()
        {
            var res = new PinanValidateCardData();
            var flag = InsuranceService.ValidateSelfServerCard("01131290000012011", "051701", ref res);
            Console.WriteLine(flag);
        }
    }
}
